package com.fiberhome.platform.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.fiberhome.platform.common.EUTreeNode;
import com.fiberhome.platform.common.PageResult;
import com.fiberhome.platform.mapper.DesignNeedMapper;
import com.fiberhome.platform.mapper.NeedCategoryMapper;
import com.fiberhome.platform.mapper.RelationMapMapper;
import com.fiberhome.platform.pojo.DesignNeedExample;
import com.fiberhome.platform.pojo.NeedCategory;
import com.fiberhome.platform.pojo.NeedCategoryExample;
import com.fiberhome.platform.pojo.RelationMapExample;
import com.fiberhome.platform.pojo.NeedCategoryExample.Criteria;
@Service
public class NeedCatServiceImpl implements NeedCatService {
	@Autowired
	private NeedCategoryMapper needCatMapper;
	@Autowired
	private DesignNeedMapper designNeedMapper;
	@Autowired
	private RelationMapMapper relationMapMapper;
	@Override
	public List<EUTreeNode> getNeedCat(Integer parentId) {
		// TODO Auto-generated method stub
		NeedCategoryExample example = new NeedCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(parentId);
		List<NeedCategory> list = needCatMapper.selectByExample(example);
		List<EUTreeNode> result = new ArrayList<EUTreeNode>();
		
		for (NeedCategory needCat : list) {
			EUTreeNode treeNode = new EUTreeNode();
			treeNode.setId(needCat.getId());
			treeNode.setText(needCat.getName());
			treeNode.setState(needCat.getIsParent()==1?"closed":"open");
			treeNode.setType(needCat.getType());
			treeNode.setDesignNo(needCat.getDesignNo());
			treeNode.setParentId(needCat.getParentId());
			treeNode.setIsParent(needCat.getIsParent());
			treeNode.setIsPon(needCat.getIsPon());
			result.add(treeNode);
		}
		return result;
	}
	
	@Override
	@Transactional
	public PageResult createNeed(NeedCategory need) {
		// TODO Auto-generated method stub
		
		NeedCategory needCat = new NeedCategory();
		needCat.setParentId(need.getParentId());
		

		//修改叶节点的text，还要更新到详情中的最后一级中的值
		needCat.setName(need.getName());
		needCat.setSortOrder(1);
		needCat.setIsParent(0);
		needCat.setCreated(new Date());
		needCat.setUpdated(new Date());
		needCat.setStatus(1);
		needCat.setType(0);
		needCat.setIsPon(need.getIsPon());
		NeedCategory parentCat = needCatMapper.selectByPrimaryKey(need.getParentId());
		if (!(parentCat.getIsParent()==1)) {
			parentCat.setIsParent(1);
			needCatMapper.updateByPrimaryKey(parentCat);
		}
		needCatMapper.insert(needCat);
		return PageResult.ok(needCat);
		
	}

	@Override
	@Transactional
	public PageResult updateNeed(NeedCategory needCat) {
//		//如果更新的是叶节点，则会同步更新详情
//		if (needCat.getIsParent()==0 && needCat.getDesignNo()!=null && needCat.getDesignNo()!="") {
//			//获取叶节点上的所有父节点，目的是为了定位最后一级的位置，从而更新最后一级的text
//			NeedCategoryExample example = new NeedCategoryExample();
//			Criteria criteria = example.createCriteria();
//			criteria.andDesignNoEqualTo(needCat.getDesignNo());
//			List<NeedCategory> needCats = needCatMapper.selectByExample(example);
//			
//			List<NeedCategory> parentList = needCatMapper.selectAllParent(needCats.get(0).getId());
//			
//			//根据designNo获取详情
//			DesignNeedExample example_need = new DesignNeedExample();
//			example_need.createCriteria().andDesignNoEqualTo(needCat.getDesignNo());
//			List<DesignNeedOut> need_detail = designNeedMapper.selectByExample(example_need);
//			DesignNeed need = need_detail.get(0);
//			int size = parentList.size();
//			
//			if (size==0) {
//				
//			} else if (size==1) {
//				need.setNeedClass1(needCat.getName());
//			} else if (size==2) {
//				need.setNeedClass2(needCat.getName());
//			} else if (size==3) {
//				need.setNeedClass3(needCat.getName());
//			} else if (size==4) {
//				need.setNeedClass4(needCat.getName());
//			} else if (size==5) {
//				need.setNeedClass5(needCat.getName());
//			}
//			designNeedMapper.updateByPrimaryKeySelective(need);
//		} else if (needCat.getIsParent()==1) {//如果是父节点就更新所有该父节点下面的详情
//			
//		}
//		
//		
//		
//		
//		
		
		needCat.setUpdated(new Date());
		int updateStat = needCatMapper.updateByPrimaryKeySelective(needCat);
		//System.out.println(updateStat);
		if (updateStat > 0) {
			return PageResult.ok(needCat);
		}
		return PageResult.ok(needCat);
	}

	
	
	
	
	
	@Override
	@Transactional
	public PageResult deleteNeed(Integer id, String designNo) {
		if (designNo==null) {
			//删除分类节点
			NeedCategory selectCon = needCatMapper.selectByPrimaryKey(id);
			Integer parentId = selectCon.getParentId();
			NeedCategory parentCon = needCatMapper.selectByPrimaryKey(parentId);
			deleteNowAndChildNode(id);
			if (!hasChildNode(parentId)) {
				parentCon.setIsParent(0);
				needCatMapper.updateByPrimaryKey(parentCon);
			}
			return PageResult.ok(selectCon);
		} else {
			//删除根节点对应的设计需求详情记录
			DesignNeedExample example = new DesignNeedExample();
			
			example.createCriteria().andDesignNoEqualTo(designNo);
			designNeedMapper.deleteByExample(example);
			
			
			//删除包含此条记录的映射表中的记录
			RelationMapExample map_exmaple = new RelationMapExample();
			map_exmaple.createCriteria().andDesignNoEqualTo(designNo);
			relationMapMapper.deleteByExample(map_exmaple);
			
			
			//删除分类节点
			NeedCategory selectCon = needCatMapper.selectByPrimaryKey(id);
			Integer parentId = selectCon.getParentId();
			NeedCategory parentCon = needCatMapper.selectByPrimaryKey(parentId);
			deleteNowAndChildNode(id);
			if (!hasChildNode(parentId)) {
				parentCon.setIsParent(0);
				needCatMapper.updateByPrimaryKey(parentCon);
			}
			return PageResult.ok(selectCon);
		}
	}
	
	@Transactional
	private void deleteNowAndChildNode(Integer id) {
		NeedCategory selectCon = needCatMapper.selectByPrimaryKey(id);
		//如果当前节点是父节点，则选出所有子节点
		if (selectCon.getIsParent()==1) {
			NeedCategoryExample example = new NeedCategoryExample();
			Criteria criteria = example.createCriteria();
			criteria.andParentIdEqualTo(id);
			List<NeedCategory> childList = needCatMapper.selectByExample(example);
			for (NeedCategory child : childList) {
				deleteNowAndChildNode(child.getId());
			}
			//子节点删除完成之后，本节点也要删除
			needCatMapper.deleteByPrimaryKey(id);
			
		} else {
			needCatMapper.deleteByPrimaryKey(id);
		}
	}
	
	private boolean hasChildNode(Integer id) {
		NeedCategoryExample example = new NeedCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(id);
		List<NeedCategory> childList = needCatMapper.selectByExample(example);
		if (childList.size() == 0) {
			return false;
		}
		return true;
	}

	@Override
	public NeedCategory getById(Integer id) {
		NeedCategory need = needCatMapper.selectByPrimaryKey(id);
		return need;
	}


}
